\XeTeXgenerateactualtext=1
\ifdefined\HANDOUT
  \documentclass[fontset=none,handout]{ctexbeamer}
\else
  \documentclass[fontset=none]{ctexbeamer}
\fi

\usepackage{%
  array,
  bookmark,
  booktabs,
  datetime2,
  fdulogo,
  fontawesome5,
  hologo,
  listings,
  multicol,
  qrcode,
  ragged2e,
  siunitx,
  xeCJKfntef,
  unicode-math}

\makeatletter

\@namedef{ver@beamerfontthememetropolis.sty}{9999/99/99}

% Beamer theme
\usetheme{Xiaoshan}
\usefonttheme{serif,professionalfonts}

% Beamer settings
\metroset{progressbar=none}
\setbeamerfont{title}{size=\huge, series=\bfseries}
\setbeamerfont*{subtitle}{size=\large, shape=\itshape}
\setbeamerfont{section title}{size=\Large, series=\bfseries}
\setbeamerfont{frametitle}{size=\large, series=\bfseries}
\setbeamerfont{caption}{size=\footnotesize, series=\bfseries}
\setbeamerfont{itemize/enumerate subbody}{size=\footnotesize}
\setbeamerfont{footnote}{size=\tiny}
\setbeamerfont{alerted text}{series=\bfseries}
\addtobeamertemplate{institute}{\raggedleft}{}
\setbeamertemplate{title}{%
  \raggedleft
  \linespread{1}%
  \inserttitle
  \hspace*{1.2cm}\par
  \vspace*{0.5em}}
\setbeamertemplate{subtitle}{%
  \raggedleft
  \insertsubtitle
  \hspace*{1.2cm}\par
  \vspace*{0.5em}}
\setbeamertemplate{title page}{%
  \begin{minipage}[b]{\textwidth}
    \usebeamertemplate*{title graphic}
    \usebeamertemplate*{title}
    \usebeamertemplate*{subtitle}
    \usebeamertemplate*{title separator}
    \usebeamertemplate*{author}
    \usebeamertemplate*{date}
    \usebeamertemplate*{institute}
    \vfill
  \end{minipage}}
\setbeamertemplate{title graphic}{%
  \vbox to 0pt {\smash{\raisebox{-128pt}{\inserttitlegraphic}}}%
  \nointerlineskip}
\setbeamertemplate{frame numbering}{\zhnumber[style=Financial]{\insertframenumber}}
\setbeamertemplate{caption}{\parbox{\textwidth}{\centering\insertcaption}\par}
\setbeamertemplate{bibliography item}[text]

% Colors
\colorlet{keyword}{松花绿}
\colorlet{comment}{漆黑!50}
\colorlet{texcs}{酡红}
\colorlet{emph1}{靛蓝}
\colorlet{emph2}{琥珀}
\colorlet{inline}{玄色}

% Fonts: basic
\setmainfont{LibertinusSerif}[
  Extension      = .otf,
  UprightFont    = *-Regular,
  BoldFont       = *-Bold,
  ItalicFont     = *-Italic,
  BoldItalicFont = *-BoldItalic,
  Scale          = 1.1]
\setmonofont{RobotoMono}[
  Extension      = .otf,
  UprightFont    = *-Regular,
  BoldFont       = *-Bold,
  ItalicFont     = *-Italic,
  BoldItalicFont = *-BoldItalic,
  Scale          = 0.92]
\setmathfont{LibertinusMath-Regular.otf}

\IfFontExistsTF{Source Han Serif SC}{
  \setCJKmainfont{Source Han Serif SC}[
    UprightFont    = * SemiBold,
    BoldFont       = * Heavy,
    ItalicFont     = * SemiBold,
    BoldItalicFont = * Heavy,
    CharacterWidth = Full]
}{
  \setCJKmainfont{Noto Serif CJK SC}[
    UprightFont    = * SemiBold,
    BoldFont       = * Black,
    ItalicFont     = * SemiBold,
    BoldItalicFont = * Black,
    CharacterWidth = Full]
}
\IfFontExistsTF{Source Han Serif SC}{
  \setCJKmonofont{Source Han Sans SC}[AutoFakeSlant]
}{
  \setCJKmonofont{Noto Sans CJK SC}[AutoFakeSlant]
}

% Fonts: TeX Live
\newfontfamily\Garamond{EBGaramond}[
  Extension      = .otf,
  UprightFont    = *-Regular,
  BoldFont       = *-Bold,
  ItalicFont     = *-Italic,
  BoldItalicFont = *-BoldItalic]
\newfontface\ArphicKai{gkai00mp.ttf}[AutoFakeBold=4, AutoFakeSlant]
\newfontface\Times{FreeSerif.otf}
\newfontface\Helvetica{FreeSans.otf}
\newfontface\Courier{FreeMono.otf}
\newfontface\Bodini{QTBodini.otf}
\newfontface\Futura{QTFuture.otf}
\newfontface\Optima{QTOptimum.otf}
\newfontface\Inconsolata{Inconsolatazi4-Regular.otf}
\newfontface\LibertinusKey{LibertinusKeyboard-Regular.otf}
\newfontface\LatinRomanV{lmroman5-regular.otf}
\newfontface\LatinRomanVI{lmroman6-regular.otf}
\newfontface\LatinRomanVII{lmroman7-regular.otf}
\newfontface\LatinRomanVIII{lmroman8-regular.otf}
\newfontface\LatinRomanIX{lmroman9-regular.otf}
\newfontface\LatinRomanX{lmroman10-regular.otf}
\newfontface\LatinRomanXII{lmroman12-regular.otf}
\newfontface\LatinRomanXVII{lmroman17-regular.otf}
\newCJKfontfamily\fangsong{FandolFang-Regular.otf}
\newCJKfontfamily\heiti{FandolHei-Regular.otf}
\newCJKfontfamily\kaishu{FandolKai-Regular.otf}

% Fonts: local
\newfontface\NotoDevanagari{NotoSerifDevanagari-Regular.otf}[Path=./fonts/, Script=Devanagari]
\newfontface\NotoArabic{NotoNaskhArabic-Regular.otf}[Path=./fonts/, Script=Arabic]
\newfontface\NotoEgyptian{NotoSansEgyptianHieroglyphs-Regular.otf}[Path=./fonts/]
\newfontface\Unifont{unifont-14.0.02.subset.ttf}[Path=./fonts/]
\newfontface\XKCD{xkcd-script.ttf}[Path=./fonts/, Ligatures=TeX]

% PDF bookmark
\apptocmd{\beamer@@frametitle}{%
  \only<1>{\expandafter\ifnum\insertcontinuationcount<2\relax
    \bookmark[page=\the\c@page,level=4]{#1}\fi}}{}{}

% Code listings
\lstdefinestyle{style@latex}{
  language     = [latex]TeX,
  alsoletter   = {*},
  keywordstyle = \bfseries\color{keyword},
  commentstyle = \itshape\color{comment},
  texcsstyle   = *\color{texcs},
  emphstyle    = [1]\itshape\color{emph1},
  emphstyle    = [2]\color{emph2}
}
\lstdefinestyle{style@inline}{
  basicstyle   = \ttfamily\color{inline},
  keepspaces   = true
}
\lstnewenvironment{texcode}[1][]{\lstset{
  style        = style@latex,
  basicstyle   = \footnotesize\ttfamily,
  gobble       = 2,
  morekeywords = {\documentclass,\usepackage,\begin,\end},#1}}{}
\lstMakeShortInline[style=style@inline]|

% Hack
% Use small caps for LaTeX symbol
\DeclareRobustCommand{\LaTeX}{%
  L\kern-.3em%
  \raisebox{.2em}{\textsc{a}}\kern-.14em%
  \TeX}
% Compatibility with unicode-math
\DeclareRobustCommand{\LaTeXe}{%
  \LaTeX\kern.15em2%
  \hbox{%
    \if b\expandafter\@car\f@series\@nil
      $_{\textstyle\symbf{\varepsilon}}$%
    \else
      $_{\textstyle\varepsilon}$%
    \fi}}
% PoZheHao, see https://github.com/CTeX-org/ctex-kit/issues/382
\ExplSyntaxOn
\xeCJK_new_class:n { PoZheHao }
\__xeCJK_save_CJK_class:n { PoZheHao }
\xeCJK_declare_char_class:nn { PoZheHao } { "2014 }
\seq_map_inline:Nn \g__xeCJK_class_seq
  {
    \str_if_eq:nnF {#1} { PoZheHao }
      {
        \xeCJK_copy_inter_class_toks:nnnn { PoZheHao } {#1} { FullRight } {#1}
        \xeCJK_copy_inter_class_toks:nnnn {#1} { PoZheHao } {#1} { FullRight }
      }
  }
\ExplSyntaxOff

% Commands
\newcommand{\link}[1]{\href{#1}{\faLink}}
\newcommand{\CASE}[1]{{\addfontfeatures{Letters=Uppercase}#1}}
\newcommand{\jatext}[1]{{\addCJKfontfeatures{Language=Japanese}#1}}
\newcommand{\zhparen}[1]{（\raisebox{0.1ex}{#1}）}
\newcommand{\enparen}[1]{\CASE{(}#1\CASE{)}}
\newcommand{\pkg}[1]{\texttt{#1}}
\newcommand{\usv}[1]{\texttt{U+#1}}
\newcommand{\kbd}[1]{{\LibertinusKey#1}}
\newcommand{\nonumberfootnote}[2][]{%
  \let\thefootnote\relax
  \footnotetext#1{#2}}
\renewcommand{\footnoterule}{}

\newcommand{\XeTeX}{\hologo{XeTeX}}
\newcommand{\pdfTeX}{\hologo{pdfTeX}}
\newcommand{\LuaTeX}{\hologo{LuaTeX}}
\newcommand{\XeLaTeX}{\hologo{Xe}\kern-.13em\LaTeX{}}
\newcommand{\pdfLaTeX}{pdf\LaTeX{}}
\newcommand{\LuaLaTeX}{Lua\LaTeX{}}
\newcommand{\BibTeX}{\hologo{BibTeX}}

\makeatother

% Information
\title{现代 \LaTeX{} 入门讲座}
\subtitle{Modern \LaTeX{} in a Nutshell}
\author{曾祥东}
\institute{复旦大学\quad 物理系}
\date{\zhtoday}
\titlegraphic{\qrcode[hyperlink, height=1.6cm]{https://github.com/stone-zeng/latex-talk}}

\begin{document}

\maketitle

\input{includes/introduction.tex}
\input{includes/install.tex}
\input{includes/start.tex}
\input{includes/content.tex}
\input{includes/templates.tex}
\input{includes/math.tex}
\input{includes/typography.tex}
\input{includes/advanced.tex}
\input{includes/about.tex}

\end{document}
